package org.cuijq.thinking.in.spring.ioc.container;

import org.cuijq.thinking.in.spring.ioc.domain.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * AnnotationApplicationContextAsIocContainerDemo 容器
 */
@Configuration
public class AnnotationApplicationContextAsIocContainerDemo {
    public static void main(String[] args) {
        //创建BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //将当前类作为配置类
        applicationContext.register(AnnotationApplicationContextAsIocContainerDemo.class);
        applicationContext.refresh();

        lookupByCollectType(applicationContext);
        applicationContext.close();
    }
    @Bean
    public User user(){
        User user=new User();
        user.setId(1l);
        user.setName("dddd");
        return user;
    }

    private static void lookupByCollectType(BeanFactory beanFactory) {
        if (beanFactory instanceof ListableBeanFactory){
            ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
            Map<String , User> users=listableBeanFactory.getBeansOfType(User.class);
            System.out.println("查找所有的User 对象"+users);
        }
    }
}
